﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ActiproSoftware.Windows.Controls.PropertyGrid;
using ActiproSoftware.Windows.Controls.PropertyGrid.Primitives;

namespace EditorExe
{
    /// <summary>
    /// Interaction logic for EvaluateArgumentsDialog.xaml
    /// </summary>
    public partial class EvaluateArgumentsDialog : Window
    {
        private readonly LambdaExpression _lambdaExpression;

        public EvaluateArgumentsDialog(LambdaExpression lambdaExpression)
        {
            _lambdaExpression = lambdaExpression;
            InitializeComponent();

            var selectedObjects = (from p in lambdaExpression.Parameters select new { Name = p.Name + "(" + p.Type + ")", Value = "" });


            //this.propgd.SelectedObject = selectedObjects;
            //this.propgd.DataFactory = new DictionaryDataFactory();
            foreach (var selectedObject in selectedObjects)
            {
                this.propgd.Items.Add(new PropertyGridPropertyItem()
                                          {
                                              DisplayName = selectedObject.Name, Value = selectedObject.Value
                                          });
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var arguments = new object[this.propgd.Items.Count];

            for (int i = 0; i < arguments.Length; i++)
            {
                arguments[i] = ((PropertyGridPropertyItem) this.propgd.Items[i]).Value;
            }

            var result = _lambdaExpression.Compile().DynamicInvoke(arguments);
            this.textBlock1.Text = result.ToString();
        }
    }
}
